using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using DAO;
using System.Data;

namespace BUS
{
    /// <summary> 
    ///Author: nnamthach@gmail.com 
    /// <summary>
    public class PurchaseRequisitionControl
    {
        #region Local Variable
        private PurchaseRequisitionDataAccess _PurchaseRequisitionDAO;
        #endregion Local Variable

        #region Method
        public PurchaseRequisitionControl()
        {
            _PurchaseRequisitionDAO = new PurchaseRequisitionDataAccess();
        }

        public PurchaseRequisitionInfo Get_PurchaseRequisition(
            Int32 Reference
        , ref string sErr)
        {
            return _PurchaseRequisitionDAO.Get_PurchaseRequisition(
            Reference
            , ref sErr);
        }

        public DataTable GetAll_PurchaseRequisition(ref string sErr)
        {
            return _PurchaseRequisitionDAO.GetAll_PurchaseRequisition(ref sErr);
        }

        public Int32 Add_PurchaseRequisition(PurchaseRequisitionInfo obj, ref string sErr)
        {
            return _PurchaseRequisitionDAO.Add_PurchaseRequisition(obj, ref sErr);
        }

        public Int32 Add_PurchaseRequisition1(PurchaseRequisitionInfo obj, ref string sErr)
        {
            return _PurchaseRequisitionDAO.Add_PurchaseRequisition1(obj, ref sErr);
        }

        public string Update_PurchaseRequisition(PurchaseRequisitionInfo obj)
        {
            return _PurchaseRequisitionDAO.Update_PurchaseRequisition(obj);
        }

        public string Update_PurchaseRequisition1(PurchaseRequisitionInfo obj)
        {
            return _PurchaseRequisitionDAO.Update_PurchaseRequisition1(obj);
        }

        public string Delete_PurchaseRequisition(
            Int32 Reference
        )
        {
            return _PurchaseRequisitionDAO.Delete_PurchaseRequisition(
            Reference
            );
        }
        public string Delete_PurchaseRequisition1(
            Int32 Reference
        )
        {
            return _PurchaseRequisitionDAO.Delete_PurchaseRequisition1(
            Reference
            );
        }

        public Boolean IsExist_PurchaseRequisition(
            Int32 Reference
        )
        {
            return _PurchaseRequisitionDAO.IsExist_PurchaseRequisition(
            Reference
            );
        }

        public Boolean IsExist_ReqRef(
            Int32 Reference, String InvNo
        )
        {
            return _PurchaseRequisitionDAO.IsExist_ReqRef(
            Reference, InvNo
            );
        }

        public Boolean IsExist_PurchaseRequisition1(
            Int32 Reference
        )
        {
            return _PurchaseRequisitionDAO.IsExist_PurchaseRequisition1(
            Reference
            );
        }

        /*  public string Delete_PurchaseRequisition(String arrID)
          {
              string kq = "";
              string[] arrStrID = arrID.Split(',');
              foreach (string strID in arrStrID)
              {
                  if (strID != "")
                  {
                      int ID = Convert.ToInt32(strID);
                      kq += _PurchaseRequisitionDAO.Delete_PurchaseRequisition(ID);
                  }
              }
              return kq;
          } */

        public DataTable Search(String columnName, String columnValue, String condition, ref String sErr)
        {
            return _PurchaseRequisitionDAO.Search(columnName, columnValue, condition, ref  sErr);
        }

        public DataTable GetCond(String strCond, ref string sErr)
        {
            return _PurchaseRequisitionDAO.GetCond(strCond, ref sErr);
        }

        public DataTable GetCond1(String strCond, ref string sErr)
        {
            return _PurchaseRequisitionDAO.GetCond1(strCond, ref sErr);
        }

        public Boolean IsExist_BudgetShared(
            String Contract,
            Int32 aYear
        )
        {
            return _PurchaseRequisitionDAO.IsExist_BudgetShared(Contract, aYear);
        }

        public Boolean IsExist_BudgetShared1(
            String Contract,
            Int32 aYear
        )
        {
            return _PurchaseRequisitionDAO.IsExist_BudgetShared1(Contract, aYear);
        }

        public DataTable GetRate(String Currency, DateTime IssueDate, ref string sErr)
        {
            return _PurchaseRequisitionDAO.GetRate(Currency, IssueDate, ref sErr);
        }

        public DataTable GetRate1(String Currency, DateTime IssueDate, ref string sErr)
        {
            return _PurchaseRequisitionDAO.GetRate1(Currency, IssueDate, ref sErr);
        }

        public DataTable GetSumAmt(String strCond, ref string sErr)
        {
            return _PurchaseRequisitionDAO.GetSumAmt(strCond, ref sErr);
        }

        public DataTable GetSumAmt1(String strCond, ref string sErr)
        {
            return _PurchaseRequisitionDAO.GetSumAmt1(strCond, ref sErr);
        }

        #endregion Method


        public string Delete(string busId, int refer)
        {
            if (busId == "HL") return _PurchaseRequisitionDAO.Delete_PurchaseRequisition(refer);
            return _PurchaseRequisitionDAO.Delete_PurchaseRequisition1(refer);
        }

        public int Add(string busId, PurchaseRequisitionInfo info, ref string sErr)
        {
            if (busId == "HL") return _PurchaseRequisitionDAO.Add_PurchaseRequisition(info, ref sErr);
            return _PurchaseRequisitionDAO.Add_PurchaseRequisition1(info, ref sErr);
        }

        public string Update(string busId, PurchaseRequisitionInfo info)
        {
            if (busId == "HL") return _PurchaseRequisitionDAO.Update_PurchaseRequisition(info);
            return _PurchaseRequisitionDAO.Update_PurchaseRequisition1(info);
        }

        public bool IsExist(string busId, int refer)
        {
            if (busId == "HL") return _PurchaseRequisitionDAO.IsExist_PurchaseRequisition(refer);
            return _PurchaseRequisitionDAO.IsExist_PurchaseRequisition1(refer);
        }

        public bool IsExist_ReqRef(string busId, string refer, int year, int sysRef)
        {
            if (busId == "HL") return _PurchaseRequisitionDAO.IsExist_ReqRefHL(refer, year, sysRef);
            return _PurchaseRequisitionDAO.IsExist_ReqRefHV(refer, year, sysRef);
        }

        public decimal getConvRate(string busId, DateTime dateTime, ref string sErr)
        {
            if (busId == "HL") return _PurchaseRequisitionDAO.getConvRateHL(dateTime, ref sErr);
            return _PurchaseRequisitionDAO.getConvRateHV(dateTime, ref sErr);
        }

        public DataTable getAmts(string busId, string refer, int year, ref string sErr)
        {
            if (busId == "HL") return _PurchaseRequisitionDAO.getAmtsHL(refer, year, ref sErr);
            return _PurchaseRequisitionDAO.getAmtsHV(refer, year, ref sErr);
        }

        public DataTable getOtherPurchaseByRef(string busId, string refer, int outYear, ref string sErr)
        {
            DataTable ret = null;
            DataTable other = null;
            if (busId == "HL")
            {
                ret = _PurchaseRequisitionDAO.getOtherPurchaseByRefHL(refer, outYear, ref sErr);
                other = _PurchaseRequisitionDAO.getOtherPurchaseByRefHV(refer, outYear, ref sErr);
            }
            else
            {
                other = _PurchaseRequisitionDAO.getOtherPurchaseByRefHL(refer, outYear, ref sErr);
                ret = _PurchaseRequisitionDAO.getOtherPurchaseByRefHV(refer, outYear, ref sErr);
            }
            ret.Columns.Add("AmtOther", typeof(decimal));
            ret.Columns.Add("OAmtOther", typeof(decimal));
            for (int i = 0; i < ret.Rows.Count; i++)
            {
                for (int j = other.Rows.Count - 1; j >= 0; j--)
                {
                    if (ret.Rows[i]["aYear"].Equals(other.Rows[j]["aYear"]))
                    {
                        ret.Rows[i]["OReference"] = other.Rows[j]["Reference"];
                        ret.Rows[i]["AmtOther"] = other.Rows[j]["Amt"];
                        ret.Rows[i]["OAmtOther"] = other.Rows[j]["OtherAmt"];
                        other.Rows.RemoveAt(j);
                    }
                }

            }
            //if (other.Rows.Count > 0)
            //{
            //    for (int j = other.Rows.Count - 1; j >= 0; j--)
            //    {
            //        DataRow newRow = ret.NewRow();
            //        newRow["aYear"] = other.Rows[j]["aYear"];
            //        newRow["Amt"] = 0;
            //        newRow["OtherAmt"] = 0;
            //        newRow["AmtOther"] = other.Rows[j]["Amt"];
            //        newRow["OAmtOther"] = other.Rows[j]["OtherAmt"];
            //    }
            //}
            return ret;
        }
        public object BeginTransaction(ref string sErr)
        {
            return _PurchaseRequisitionDAO.BeginTransaction(ref sErr);
        }
        public object CommitTransaction(ref string sErr)
        {
            return _PurchaseRequisitionDAO.CommitTransaction(ref sErr);
        }
        public object RollbackTransaction(ref string sErr)
        {
            return _PurchaseRequisitionDAO.RollbackTransaction(ref sErr);
        }


        public string DeleteRef(string busId, string refer, int year)
        {
            if (busId == "HL") return _PurchaseRequisitionDAO.deleteRefHL(refer, year);
            return _PurchaseRequisitionDAO.deleteRefHV(refer, year);
        }

        public void insertUpdateAmt(string busId, PurchaseRequisitionInfo info, ref string sErr)
        {
            if (busId == "HL")
            {
                if (_PurchaseRequisitionDAO.IsExist_ReqRefHL(info.InvNo, info.aYear)) sErr = _PurchaseRequisitionDAO.updateAmtHL(info);
                else _PurchaseRequisitionDAO.Add_PurchaseRequisition(info, ref sErr);
            }
            else
            {
                if (_PurchaseRequisitionDAO.IsExist_ReqRefHV(info.InvNo, info.aYear)) sErr = _PurchaseRequisitionDAO.updateAmtHV(info);
                else _PurchaseRequisitionDAO.Add_PurchaseRequisition1(info, ref sErr);
            }
        }
        public string updateAmt(string busId, PurchaseRequisitionInfo info)
        {
            if (busId == "HL") return _PurchaseRequisitionDAO.updateAmtHL(info);
            return _PurchaseRequisitionDAO.updateAmtHV(info);

        }

        public DataTable GetShortAll(string busId, ref string sErr)
        {
            if (busId == "HL") return _PurchaseRequisitionDAO.GetShortAllHL(ref  sErr);
            return _PurchaseRequisitionDAO.GetShortAllHV(ref  sErr);
        }

        public decimal getBudgetTotalAmt(string busId, string code, int year, ref string sErr, int refer = -1)
        {
            if (busId == "HL") return _PurchaseRequisitionDAO.getBudgetTotalAmtHL(code, year, ref  sErr, refer);
            return _PurchaseRequisitionDAO.getBudgetTotalAmtHV(code, year, ref  sErr, refer);
        }
        public DataTable GetCond(string busId, String strCond, ref string sErr)
        {
            if (busId == "HL") return GetCond(strCond, ref  sErr);
            return GetCond1(strCond, ref  sErr);
        }




        public decimal getBudgetGroupTotalAmt(string busId, string code, int year, ref string sErr, int refer = -1)
        {
            if (busId == "HL") return _PurchaseRequisitionDAO.getBudgetGroupTotalAmtHL(code, year, ref  sErr, refer);
            return _PurchaseRequisitionDAO.getBudgetGroupTotalAmtHV(code, year, ref  sErr, refer);
        }
    }
}
